<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Gated Graph Neural Networks &mdash; Graph4NLP v0.4.1 documentation</title><link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
  <!--[if lt IE 9]>
    <script src="../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  <script id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/jquery.js"></script>
        <script src="../../_static/underscore.js"></script>
        <script src="../../_static/doctools.js"></script>
        <script src="../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <script src="../../_static/js/theme.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="Graph Attention Networks" href="gat.html" />
    <link rel="prev" title="Graph Convolutional Networks" href="gcn.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
            <a href="../../index.html" class="icon icon-home"> Graph4NLP
          </a>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <p class="caption"><span class="caption-text">Get Started</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../welcome/installation.html">Install Graph4NLP</a></li>
</ul>
<p class="caption"><span class="caption-text">User Guide</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../graphdata.html">Chapter 1. Graph Data</a></li>
<li class="toctree-l1"><a class="reference internal" href="../dataset.html">Chapter 2. Dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="../construction.html">Chapter 3. Graph Construction</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../gnn.html">Chapter 4. Graph Encoder</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="gcn.html">Graph Convolutional Networks</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Gated Graph Neural Networks</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#ggnn-module-construction-function">4.2.1 GGNN Module Construction Function</a></li>
<li class="toctree-l3"><a class="reference internal" href="#ggnnlayer-construction-function">4.2.2 GGNNLayer Construction Function</a></li>
<li class="toctree-l3"><a class="reference internal" href="#ggnnlayerconv-construction-function">4.2.3 GGNNLayerConv Construction Function</a></li>
<li class="toctree-l3"><a class="reference internal" href="#ggnn-forward-function">4.2.4 GGNN Forward Function</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="gat.html">Graph Attention Networks</a></li>
<li class="toctree-l2"><a class="reference internal" href="graphsage.html">GraphSAGE</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../decoding.html">Chapter 5. Decoder</a></li>
<li class="toctree-l1"><a class="reference internal" href="../classification.html">Chapter 6. Classification</a></li>
<li class="toctree-l1"><a class="reference internal" href="../evaluation.html">Chapter 7. Evaluations and Loss components</a></li>
</ul>
<p class="caption"><span class="caption-text">Module API references</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../modules/data.html">graph4nlp.data</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules/datasets.html">graph4nlp.datasets</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules/graph_construction.html">graph4nlp.graph_construction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules/graph_embedding.html">graph4nlp.graph_embedding</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules/prediction.html">graph4nlp.prediction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules/loss.html">graph4nlp.loss</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules/evaluation.html">graph4nlp.evaluation</a></li>
</ul>
<p class="caption"><span class="caption-text">Tutorials</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../tutorial/text_classification.html">Text Classification Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../tutorial/semantic_parsing.html">Semantic Parsing Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../tutorial/math_word_problem.html">Math Word Problem Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../tutorial/knowledge_graph_completion.html">Knowledge Graph Completion Tutorial</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">Graph4NLP</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../../index.html" class="icon icon-home"></a> &raquo;</li>
          <li><a href="../gnn.html">Chapter 4. Graph Encoder</a> &raquo;</li>
      <li>Gated Graph Neural Networks</li>
      <li class="wy-breadcrumbs-aside">
            <a href="../../_sources/guide/gnn/ggnn.rst.txt" rel="nofollow"> View page source</a>
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <div class="section" id="gated-graph-neural-networks">
<span id="guide-ggnn"></span><h1>Gated Graph Neural Networks<a class="headerlink" href="#gated-graph-neural-networks" title="Permalink to this headline">¶</a></h1>
<p>A typical example of recurrent-based graph filters is the Gated Graph Neural Networks (<a class="reference external" href="https://arxiv.org/pdf/1511.05493.pdf">GGNN</a>)-filter.
The biggest modification from typical GNNs to GGNNs is the use of Gated Recurrent Units (GRU).
The GGNN-filter also takes the edge type and edge direction into consideration.
To this end, <span class="math notranslate nohighlight">\(e_{i,j}\)</span> denotes the directed edge from node <span class="math notranslate nohighlight">\(v_i\)</span> to node <span class="math notranslate nohighlight">\(v_j\)</span>
and the edge type of <span class="math notranslate nohighlight">\(e_{i,j}\)</span> is <span class="math notranslate nohighlight">\(t_{i,j}\)</span>. The propagation process of recurrent-based
filter  <span class="math notranslate nohighlight">\(f_\mathbf{filter}\)</span> in GGNN can be summarized as follows:</p>
<div class="math notranslate nohighlight">
\[ \begin{align}\begin{aligned}\mathbf{h}_i^{(0)} = [\mathbf{x}_i^T, \mathbf{0}]^T\\\mathbf{a}_i^{(l)} = A_{i:}^T[\mathbf{h}_1^{(l-1)}...\mathbf{h}_n^{(l-1)}]^T\\\mathbf{h}_i^{(l)} = \text{GRU}(\mathbf{a}_i^{(l)}, \mathbf{h}_i^{(l-1)})\end{aligned}\end{align} \]</div>
<p>where <span class="math notranslate nohighlight">\(A \in \mathbb{R}^{{dn} \times 2dn}\)</span> is a matrix determining how nodes in the
graph communicating with each other. <span class="math notranslate nohighlight">\(n\)</span> is the number of nodes in the graph.
<span class="math notranslate nohighlight">\(A_{i:} \in \mathbb{R}^{d \times 2d}\)</span> are the two columns of blocks in <span class="math notranslate nohighlight">\(A\)</span>
corresponding to node <span class="math notranslate nohighlight">\(v_i\)</span>. In Eq. eqref{ggnn-0}, the initial node feature
<span class="math notranslate nohighlight">\(\mathbf{x}_i\)</span> are padded with extra zeros to make the input size equal to the
hidden size. Eq. eqref{eq:ggnn-aggregation} computes
<span class="math notranslate nohighlight">\(\mathbf{a}_i^{(l)} \in \mathbb{R}^{2d}\)</span> by aggregating information from different
nodes via incoming and outgoing edges with parameters dependent on the edge type
and direction. The following step uses a GRU unit to update the hidden state of
node <span class="math notranslate nohighlight">\(v\)</span> by incorporating <span class="math notranslate nohighlight">\(\mathbf{a}_i^{(l)}\)</span> and the previous timestep hidden
state <span class="math notranslate nohighlight">\(\mathbf{h}_i^{(l-1)}\)</span>.</p>
<div class="section" id="ggnn-module-construction-function">
<h2>4.2.1 GGNN Module Construction Function<a class="headerlink" href="#ggnn-module-construction-function" title="Permalink to this headline">¶</a></h2>
<p>The construction function performs the following steps:</p>
<ol class="arabic simple">
<li><p>Set options.</p></li>
<li><p>Register learnable parameters or submodules (<code class="docutils literal notranslate"><span class="pre">GGNNLayer</span></code>).</p></li>
</ol>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">GGNN</span><span class="p">(</span><span class="n">GNNBase</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">num_layers</span><span class="p">,</span> <span class="n">input_size</span><span class="p">,</span> <span class="n">hidden_size</span><span class="p">,</span> <span class="n">output_size</span><span class="p">,</span> <span class="n">feat_drop</span><span class="o">=</span><span class="mf">0.</span><span class="p">,</span>
                 <span class="n">direction_option</span><span class="o">=</span><span class="s1">&#39;bi_fuse&#39;</span><span class="p">,</span> <span class="n">n_etypes</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">bias</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">use_edge_weight</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">GGNN</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">num_layers</span> <span class="o">=</span> <span class="n">num_layers</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">direction_option</span> <span class="o">=</span> <span class="n">direction_option</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">input_size</span> <span class="o">=</span> <span class="n">input_size</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">output_size</span> <span class="o">=</span> <span class="n">output_size</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">feat_drop</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Dropout</span><span class="p">(</span><span class="n">feat_drop</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">use_edge_weight</span> <span class="o">=</span> <span class="n">use_edge_weight</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">n_etypes</span> <span class="o">=</span> <span class="n">n_etypes</span>

        <span class="k">assert</span> <span class="bp">self</span><span class="o">.</span><span class="n">output_size</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">input_size</span>
        <span class="k">assert</span> <span class="bp">self</span><span class="o">.</span><span class="n">output_size</span> <span class="o">==</span> <span class="n">hidden_size</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">direction_option</span> <span class="o">==</span> <span class="s1">&#39;undirected&#39;</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">models</span> <span class="o">=</span> <span class="n">GGNNLayer</span><span class="p">(</span><span class="n">input_size</span><span class="p">,</span> <span class="n">output_size</span><span class="p">,</span> <span class="n">direction_option</span><span class="p">,</span> <span class="n">num_layers</span><span class="o">=</span><span class="n">num_layers</span><span class="p">,</span> <span class="n">n_etypes</span><span class="o">=</span><span class="n">n_etypes</span><span class="p">,</span>
                                    <span class="n">bias</span><span class="o">=</span><span class="n">bias</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">models</span> <span class="o">=</span> <span class="n">GGNNLayer</span><span class="p">(</span><span class="n">input_size</span><span class="p">,</span> <span class="n">output_size</span><span class="p">,</span> <span class="n">direction_option</span><span class="p">,</span> <span class="n">n_etypes</span><span class="o">=</span><span class="n">n_etypes</span><span class="p">,</span> <span class="n">bias</span><span class="o">=</span><span class="n">bias</span><span class="p">)</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">hidden_size</span></code> should be equal to output_size.</p>
<p><code class="docutils literal notranslate"><span class="pre">n_etypes</span></code> Number of edge types. n_etypes can be set to any integer if the direction_option is ‘undirected’.
If the direction_option is ‘bi_sep’ or ‘bi_fuse’, n_etypes will be set to 1.</p>
</div>
<div class="section" id="ggnnlayer-construction-function">
<h2>4.2.2 GGNNLayer Construction Function<a class="headerlink" href="#ggnnlayer-construction-function" title="Permalink to this headline">¶</a></h2>
<p>Similaer to <code class="docutils literal notranslate"><span class="pre">GCNLayer</span></code>, <code class="docutils literal notranslate"><span class="pre">GGNNLayer</span></code> is a single-layer GGNN and its initial options are same as class <code class="docutils literal notranslate"><span class="pre">GGNN</span></code>.
This module registers different GGNNLayerConv according to <code class="docutils literal notranslate"><span class="pre">direction_option</span></code>.</p>
</div>
<div class="section" id="ggnnlayerconv-construction-function">
<h2>4.2.3 GGNNLayerConv Construction Function<a class="headerlink" href="#ggnnlayerconv-construction-function" title="Permalink to this headline">¶</a></h2>
<p>We will take <code class="docutils literal notranslate"><span class="pre">BiSepGGNNLayerConv</span></code> as an example. The construction function performs the following steps:</p>
<ol class="arabic simple">
<li><p>Set options.</p></li>
<li><p>Register learnable parameters.</p></li>
<li><p>Reset parameters.</p></li>
</ol>
<p>The aggregation and upate functions are formulated as:</p>
<div class="math notranslate nohighlight">
\[ \begin{align}\begin{aligned}h_{i}^{0} = [ x_i \| \mathbf{0} ]\\a_{i, \vdash}^{t} = \sum_{j\in\mathcal{N}_{\vdash }(i)} W_{\vdash} h_{j, \vdash}^{t}\\a_{i, \dashv}^{t} = \sum_{j\in\mathcal{N}_{\dashv }(i)} W_{\dashv} h_{j, \dashv}^{t}\\h_{i, \vdash}^{t+1} = \mathrm{GRU}_{\vdash}(a_{i, \vdash}^{t}, h_{i, \vdash}^{t})\\h_{i, \dashv}^{t+1} = \mathrm{GRU}_{\dashv}(a_{i, \dashv}^{t}, h_{i, \dashv}^{t})\end{aligned}\end{align} \]</div>
<p>As shown in the equations, node embeddings in both directions are conveyed separately.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">BiSepGGNNLayerConv</span><span class="p">(</span><span class="n">GNNLayerBase</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">input_size</span><span class="p">,</span> <span class="n">output_size</span><span class="p">,</span> <span class="n">n_etypes</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">bias</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">BiSepGGNNLayerConv</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_input_size</span> <span class="o">=</span> <span class="n">input_size</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_output_size</span> <span class="o">=</span> <span class="n">output_size</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_n_etypes</span> <span class="o">=</span> <span class="n">n_etypes</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">linears_in</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">ModuleList</span><span class="p">(</span>
            <span class="p">[</span><span class="n">nn</span><span class="o">.</span><span class="n">Linear</span><span class="p">(</span><span class="n">output_size</span><span class="p">,</span> <span class="n">output_size</span><span class="p">)</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n_etypes</span><span class="p">)]</span>
        <span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">linears_out</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">ModuleList</span><span class="p">(</span>
            <span class="p">[</span><span class="n">nn</span><span class="o">.</span><span class="n">Linear</span><span class="p">(</span><span class="n">output_size</span><span class="p">,</span> <span class="n">output_size</span><span class="p">)</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n_etypes</span><span class="p">)]</span>
        <span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">gru_in</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">GRUCell</span><span class="p">(</span><span class="n">output_size</span><span class="p">,</span> <span class="n">output_size</span><span class="p">,</span> <span class="n">bias</span><span class="o">=</span><span class="n">bias</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">gru_out</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">GRUCell</span><span class="p">(</span><span class="n">output_size</span><span class="p">,</span> <span class="n">output_size</span><span class="p">,</span> <span class="n">bias</span><span class="o">=</span><span class="n">bias</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">reset_parameters</span><span class="p">()</span>
</pre></div>
</div>
<p>All learnable parameters and layers defined in this module are bidirectional, such as <code class="docutils literal notranslate"><span class="pre">self.gru_in</span></code> and <code class="docutils literal notranslate"><span class="pre">self.gru_out</span></code>.</p>
</div>
<div class="section" id="ggnn-forward-function">
<h2>4.2.4 GGNN Forward Function<a class="headerlink" href="#ggnn-forward-function" title="Permalink to this headline">¶</a></h2>
<p>In NN module, <code class="docutils literal notranslate"><span class="pre">forward()</span></code> function does the actual message passing and computation. <code class="docutils literal notranslate"><span class="pre">forward()</span></code> takes a parameter <code class="docutils literal notranslate"><span class="pre">GraphData</span></code> as input.</p>
<p>The rest of the section takes a deep dive into the <code class="docutils literal notranslate"><span class="pre">forward()</span></code> function.</p>
<p>We first need to obatin the input graph node features and convert the <code class="docutils literal notranslate"><span class="pre">GraphData</span></code> to <code class="docutils literal notranslate"><span class="pre">dgl.DGLGraph</span></code>. Then, we need to determine whether to expand <code class="docutils literal notranslate"><span class="pre">feat</span></code> according to <code class="docutils literal notranslate"><span class="pre">self.use_edge_weight</span></code> and whether to use edge weight according to <code class="docutils literal notranslate"><span class="pre">self.direction_option</span></code>.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">n_etypes</span><span class="o">==</span><span class="mi">1</span><span class="p">:</span>
    <span class="n">graph</span><span class="o">.</span><span class="n">edge_features</span><span class="p">[</span><span class="s1">&#39;etype&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">tensor</span><span class="p">([</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">graph</span><span class="o">.</span><span class="n">get_edge_num</span><span class="p">(),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">torch</span><span class="o">.</span><span class="n">long</span><span class="p">,</span> <span class="n">device</span><span class="o">=</span><span class="n">graph</span><span class="o">.</span><span class="n">device</span><span class="p">)</span>

<span class="n">node_feats</span> <span class="o">=</span> <span class="n">graph</span><span class="o">.</span><span class="n">node_features</span><span class="p">[</span><span class="s1">&#39;node_feat&#39;</span><span class="p">]</span>
<span class="n">etypes</span> <span class="o">=</span> <span class="n">graph</span><span class="o">.</span><span class="n">edge_features</span><span class="p">[</span><span class="s1">&#39;etype&#39;</span><span class="p">]</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">use_edge_weight</span><span class="p">:</span>
    <span class="n">edge_weight</span> <span class="o">=</span> <span class="n">graph</span><span class="o">.</span><span class="n">edge_features</span><span class="p">[</span><span class="s1">&#39;edge_weight&#39;</span><span class="p">]</span>
    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">direction_option</span> <span class="o">==</span> <span class="s1">&#39;bi_fuse&#39;</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">direction_option</span> <span class="o">==</span> <span class="s1">&#39;bi_sep&#39;</span><span class="p">:</span>
        <span class="n">reverse_edge_weight</span> <span class="o">=</span> <span class="n">graph</span><span class="o">.</span><span class="n">edge_features</span><span class="p">[</span><span class="s1">&#39;reverse_edge_weight&#39;</span><span class="p">]</span>
        <span class="n">edge_weight</span> <span class="o">=</span> <span class="p">(</span><span class="n">edge_weight</span><span class="p">,</span> <span class="n">reverse_edge_weight</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">edge_weight</span> <span class="o">=</span> <span class="kc">None</span>

<span class="n">dgl_graph</span> <span class="o">=</span> <span class="n">graph</span><span class="o">.</span><span class="n">to_dgl</span><span class="p">()</span>
</pre></div>
</div>
<p>The following code actually performs message passing and feature updating.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">direction_option</span> <span class="o">==</span> <span class="s1">&#39;undirected&#39;</span><span class="p">:</span>
    <span class="n">node_embs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">models</span><span class="p">(</span><span class="n">dgl_graph</span><span class="p">,</span> <span class="n">node_feats</span><span class="p">,</span> <span class="n">etypes</span><span class="p">,</span> <span class="n">edge_weight</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
    <span class="k">assert</span> <span class="n">node_feats</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">input_size</span>

    <span class="n">zero_pad</span> <span class="o">=</span> <span class="n">node_feats</span><span class="o">.</span><span class="n">new_zeros</span><span class="p">((</span><span class="n">node_feats</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">output_size</span> <span class="o">-</span> <span class="n">node_feats</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
    <span class="n">node_feats</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">cat</span><span class="p">([</span><span class="n">node_feats</span><span class="p">,</span> <span class="n">zero_pad</span><span class="p">],</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>

    <span class="n">feat_in</span> <span class="o">=</span> <span class="n">node_feats</span>
    <span class="n">feat_out</span> <span class="o">=</span> <span class="n">node_feats</span>

    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">num_layers</span><span class="p">):</span>
        <span class="n">feat_in</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">feat_drop</span><span class="p">(</span><span class="n">feat_in</span><span class="p">)</span>
        <span class="n">feat_out</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">feat_drop</span><span class="p">(</span><span class="n">feat_out</span><span class="p">)</span>
        <span class="n">h</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">models</span><span class="p">(</span><span class="n">dgl_graph</span><span class="p">,</span> <span class="p">(</span><span class="n">feat_in</span><span class="p">,</span> <span class="n">feat_out</span><span class="p">),</span> <span class="n">etypes</span><span class="p">,</span> <span class="n">edge_weight</span><span class="p">)</span>
        <span class="n">feat_in</span> <span class="o">=</span> <span class="n">h</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">feat_out</span> <span class="o">=</span> <span class="n">h</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>

    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">direction_option</span> <span class="o">==</span> <span class="s1">&#39;bi_sep&#39;</span><span class="p">:</span>
        <span class="n">node_embs</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">cat</span><span class="p">([</span><span class="n">feat_in</span><span class="p">,</span> <span class="n">feat_out</span><span class="p">],</span> <span class="n">dim</span><span class="o">=-</span><span class="mi">1</span><span class="p">)</span>
    <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">direction_option</span> <span class="o">==</span> <span class="s1">&#39;bi_fuse&#39;</span><span class="p">:</span>
        <span class="n">node_embs</span> <span class="o">=</span> <span class="n">feat_in</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;Unknown `bidirection` value: </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">direction_option</span><span class="p">))</span>

<span class="n">graph</span><span class="o">.</span><span class="n">node_features</span><span class="p">[</span><span class="s1">&#39;node_emb&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">node_embs</span>
</pre></div>
</div>
</div>
</div>


           </div>
          </div>
          <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
        <a href="gcn.html" class="btn btn-neutral float-left" title="Graph Convolutional Networks" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
        <a href="gat.html" class="btn btn-neutral float-right" title="Graph Attention Networks" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2020, Graph4AI Group.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>